package wechat.forward.Util;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.google.gson.Gson;

import de.robv.android.xposed.XposedBridge;
import wechat.forward.Helper.DialogHelper;
import wechat.forward.R;
import wechat.forward.XposedInit;

/**
 * Created by xuxingcan on 16-10-24.
 */
public class MainFloatUtil {
    private Gson gson;
    private Context mContext;
    private WindowManager.LayoutParams wmParams;
    private WindowManager mWindowManager;
    private LinearLayout mFloatLayout;
    private View iconButton;
    private XposedInit mXposedInit;

    public MainFloatUtil(Context mContext, XposedInit mXposedInit) {
        gson = new Gson();
        this.mContext = mContext;
        this.mXposedInit = mXposedInit;
    }

    public void clearFloatView() {
        if (mWindowManager != null && mFloatLayout != null) {
            mWindowManager.removeView(mFloatLayout);
        }
    }

    public LinearLayout getFloatLayout() {
        return mFloatLayout;
    }

    public void createFloatView() throws Exception {
        wmParams = new WindowManager.LayoutParams();
        //获取的是WindowManagerImpl.CompatModeWrapper
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        //设置window type
        wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        //设置图片格式，效果为背景透明
        wmParams.format = PixelFormat.RGBA_8888;
        //设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //调整悬浮窗显示的停靠位置为左侧置顶
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        // 以屏幕左上角为原点，设置x、y初始值，相对于gravity
        wmParams.x = mWindowManager.getDefaultDisplay().getWidth();
        wmParams.y = 150;

        //设置悬浮窗口长宽数据
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        final LayoutInflater inflater = LayoutInflater.from(mContext);
        //获取浮动窗口视图所在布局
        Context pkgContext = mContext.createPackageContext(XposedInit.pkgName, Context.CONTEXT_IGNORE_SECURITY);
        mFloatLayout = (LinearLayout) inflater.inflate(pkgContext.getResources().getLayout(R.layout.float_layout_main), null);
        iconButton = mFloatLayout.findViewById(R.id.icon);
        iconButton.setBackgroundDrawable(new BitmapDrawable(getRoundBitmap(((BitmapDrawable) pkgContext.getResources().getDrawable(R.drawable.icon)).getBitmap())));
        iconButton.setOnClickListener(listener);
        //添加mFloatLayout
        mWindowManager.addView(mFloatLayout, wmParams);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                DialogHelper.showMenuDialog(mContext, "操作", new String[]{"选择群", "选择好友", "添加群好友", "发生间隔", "发送文字", "发送收藏", "发送名片", "保存好友至SD卡"}, new DialogHelper.MenuDialogCallBack() {
                    @Override
                    public void onItemOnClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                mXposedInit.startGroupSelectUI();
                                break;
                            case 1:
                                mXposedInit.startSelectFriend();
                                break;
                        }
                    }
                });
            } catch (Exception e) {
                XposedBridge.log(e);
            }
        }
    };

    public static Bitmap getRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int r = 0;
        if (width > height) {
            r = height;
        } else {
            r = width;
        }
        Bitmap backgroundBmp = Bitmap.createBitmap(r, r, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(backgroundBmp);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        RectF rect = new RectF(0, 0, r, r);
        canvas.drawRoundRect(rect, r / 2, r / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, null, rect, paint);
        return backgroundBmp;
    }
}
